<template>
  <div class="page-filter">
    <div class="page-filter-left">
      <a-button type="primary" @click="handleAddSubClass">新建类型</a-button>
      <a-button @click="multipleEdit" v-if="!statusState.isEdit">批量编辑</a-button>
      <a-button type="info" v-else @click="complete">完成</a-button>
      <a-button danger @click="deleteType">删除</a-button>
    </div>
    <div class="page-filter-right">
      <a-select
        v-model:value.trim="typeState.categ_id"
        placeholder="所属分类"
        class="select-m-r-24"
        :getPopupContainer="(triggerNode: any) => triggerNode.parentNode"
      >
        <a-select-option :value="undefined" v-if="atlasList.length > 0">全部分类</a-select-option>
        <a-select-option :value="item.id" v-for="item in atlasList" :key="item.id">{{ item.name }}</a-select-option>
      </a-select>
      <a-input
        v-model:value.trim="typeState.keyword"
        class="input-m-r-16 w-[250px]"
        placeholder="请输入分类/类型名称关键词"
      />
      <a-button type="primary" class="m-r-8" @click="handleSearch">搜索</a-button>
      <a-button class="btn-m-r-24" @click="handleReset">重置</a-button>
    </div>
  </div>
  <!-- 删除对话框 -->
  <modal-view
    title="提示"
    v-model:visible="deleteVisible"
    okText="确定"
    cancelText="取消"
    :width="458"
    @confirm="deleteConfirm"
    @cancel="deleteCancel"
  >
    <span>确定要删除该分类吗</span>
  </modal-view>
</template>
<script lang="ts" setup>
import ModalView from '@/components/components/modal/ModalView.vue'
import { typeState, resetState, statusState } from '../state'
import type { TypeListData } from '@/types/site/article/articleType'
import { message } from 'ant-design-vue'
import type { ICopyList } from '../state'
import { onMounted, ref } from 'vue'
import apis from '@/apis'
const {
  site: { article_del_type, batchUpdate_articleType }
} = apis

const emit = defineEmits(['update-list'])
const itemArr: any = ref([])
const deleteVisible = ref(false)
defineProps<{ atlasList: ICopyList[] }>()
const deleteType = () => {
  if (!statusState.selectedRowKeys.length) return message.warning('请选择至少一个类型进行删除')
  deleteVisible.value = true
  statusState.isEdit = false
}
/**
  删除分类对话框事件
 */
const deleteConfirm = (value: any) => {
  console.log('value: ', value)
  article_del_type({ type_id: statusState.selectedRowKeys }).then(() => {
    deleteVisible.value = false
    message.success('批量删除成功')
    emit('update-list')
  })
}
const deleteCancel = () => {
  deleteVisible.value = false
  statusState.selectedRowKeys = []
}
//新建分类
const handleAddSubClass = () => {
  statusState.addTypeVisible = true
  statusState.isAdd = true
  statusState.isEdit = false
}
//批量编辑
const multipleEdit = () => {
  if (statusState.typeList.length <= 0) return message.warning('请添加至少一个类型')
  statusState.isEdit = true
  itemArr.value = []
}
//树形数据数据处理 过滤所有子节点
const parseTreeArray = (treeArray: TypeListData[]) => {
  treeArray.forEach((item: TypeListData, index: number) => {
    const element = treeArray[index]
    //判断type是否是数组
    if (item.type && typeof element.type == 'object') {
      parseTreeArray(element.type)
    } else {
      //判断是否为子节点
      if (!(element.type || typeof element.type == 'object')) {
        //获取符合的节点
        console.log('element: ', element)
        //转化为后端需要的数据结构
        itemArr.value.push({
          type_name: element.name,
          type_status: element.type_status,
          type_order: element.type_order,
          type_id: element.id
          //todo 此处deleted 根据接口看是否加
          // deleted: 0
        })
      }
    }
  })
  console.log('itemArr.value: ', itemArr.value)
}
//完成
const complete = () => {
  itemArr.value = []
  parseTreeArray(statusState.typeList)
  const list = itemArr.value
  for (var i = 0; i <= list.length; i++) {
    if (list.some((item: any) => item.type_name === '')) {
      message.warning('类型不能为空')
      return false
    }
  }
  batchUpdate_articleType({ types: itemArr.value }).then(() => {
    message.success('批量编辑成功')
    statusState.selectedRowKeys = []
    statusState.isEdit = false
    emit('update-list')
  })
}
const handleSearch = () => {
  console.log(typeState)
  emit('update-list')
}
const handleReset = () => {
  resetState()
  console.log(typeState)
  emit('update-list')
}
onMounted(() => {
  statusState.selectedRowKeys = []
  resetState()
})
</script>

<style lang="less" scoped>
@common-color: #0083ff; // sea blue

.page-filter {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;

  &-left {
    display: flex;
    flex-wrap: nowrap;
    margin-top: 10px;

    .ant-btn:not(:last-child) {
      margin-right: 16px;
    }

    .ant-btn:last-child {
      margin-right: 16px;
      background-color: rgba(254, 76, 77, 0.08);
    }
  }

  &-right {
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    .select-m-r-24 {
      width: 196px;
      height: 32px;
      margin: 10px 24px 0 0;
    }

    .input-m-r-16 {
      height: 32px;
      margin: 10px 16px 0 0;
    }

    .btn-m-r-24 {
      margin: 10px 24px 0 0;
    }

    .m-r-8 {
      margin: 10px 8px 0 0;
    }
  }
}
</style>
